<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检查对象或类是否具有该属性</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.method-exists.html">« method_exists</a></li>
      <li style="float: right;"><a href="function.trait-exists.html">trait_exists »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.classobj.html">类/对象 函数</a></li>
    <li>检查对象或类是否具有该属性</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.property-exists" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">property_exists</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">property_exists</span> &mdash; <span class="dc-title">
   检查对象或类是否具有该属性
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.property-exists-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>property_exists</strong></span>(<span class="methodparam"><span class="type"><span class="type">object</span>|<span class="type">string</span></span> <code class="parameter">$object_or_class</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$property</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   本函数检查给出的 <code class="parameter">property</code> 是否存在于指定的类中。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    跟 <span class="function"><a href="function.isset.html" class="function">isset()</a></span> 的区别是即使属性的值为 <strong><code>null</code></strong>，<span class="function"><strong>property_exists()</strong></span> 也会返回 <strong><code>true</code></strong>。
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.property-exists-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">object_or_class</code></dt>

      <dd>

       <p class="para">
        需要检查的类名或者类的对象
       </p>
      </dd>

     
    
     <dt>
<code class="parameter">property</code></dt>

      <dd>

       <p class="para">
        属性的名称
       </p>
      </dd>

     
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.property-exists-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果属性存在则返回 <strong><code>true</code></strong>，不存在则返回 <strong><code>false</code></strong>。如果发生错误则返回 <strong><code>null</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.property-exists-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5044">
    <p><strong>示例 #1 <span class="function"><strong>property_exists()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">myClass&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$mine</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$xpto</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;protected&nbsp;</span><span style="color: #0000BB">$test</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">test</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">property_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'myClass'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'xpto'</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//true<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">}<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">property_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'myClass'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mine'</span><span style="color: #007700">));&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//true<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">property_exists</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">myClass</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mine'</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//true<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">property_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'myClass'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'xpto'</span><span style="color: #007700">));&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//true<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">property_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'myClass'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">));&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//false<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">property_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'myClass'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">));&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//true<br /></span><span style="color: #0000BB">myClass</span><span style="color: #007700">::</span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.property-exists-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">如果此类不是已知类，使用此函数会使用任何已注册的
<a href="language.oop5.autoload.html" class="link">autoloader</a>。</p></p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    <span class="function"><strong>property_exists()</strong></span> 函数不能检查通过
    <a href="language.oop5.overloading.html#language.oop5.overloading.members" class="link"><code class="literal">__get</code></a>
    魔术方法访问的属性。
   </p>
  </p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.property-exists-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.method-exists.html" class="function" rel="rdfs-seeAlso">method_exists()</a> - 检查类的方法是否存在</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>